package com.stone.customer.console.api.controller.v1;

import com.stone.customer.core.domain.entity.WishList;
import com.stone.customer.core.domain.repository.WishListRepository;
import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 心愿单表 管理 API
 *
 * @author Mr_wenpan@163.com 2021-07-21 15:25:16
 */
@RestController("wishListController.v1")
@RequestMapping("/v1/{organizationId}/wish-lists")
public class WishListController extends BaseController {

    @Autowired
    private WishListRepository wishListRepository;

    @ApiOperation(value = "心愿单表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<Page<WishList>> list(WishList wishList, @ApiIgnore @SortDefault(value = WishList.FIELD_WISH_LIST_ID,
            direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<WishList> list = wishListRepository.pageAndSort(pageRequest, wishList);
        return Results.success(list);
    }

    @ApiOperation(value = "心愿单表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{wishListId}")
    public ResponseEntity<WishList> detail(@PathVariable Long wishListId) {
        WishList wishList = wishListRepository.selectByPrimaryKey(wishListId);
        return Results.success(wishList);
    }

    @ApiOperation(value = "创建心愿单表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<WishList> create(@RequestBody WishList wishList) {
        validObject(wishList);
        wishListRepository.insertSelective(wishList);
        return Results.success(wishList);
    }

    @ApiOperation(value = "修改心愿单表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PutMapping
    public ResponseEntity<WishList> update(@RequestBody WishList wishList) {
        SecurityTokenHelper.validToken(wishList);
        wishListRepository.updateByPrimaryKeySelective(wishList);
        return Results.success(wishList);
    }

    @ApiOperation(value = "删除心愿单表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> remove(@RequestBody WishList wishList) {
        SecurityTokenHelper.validToken(wishList);
        wishListRepository.deleteByPrimaryKey(wishList);
        return Results.success();
    }

}
